package com.ingta.si.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ingta.framework.ibatis.util.Page;
import com.ingta.framework.ibatis.util.QueryParams;
import com.ingta.framework.util.dwz.ReturnValue;
import com.ingta.si.admin.entity.Admin;
import com.ingta.si.admin.service.AdminService;

/**
 * 用户权限管理controller
 *
 * @author ingta_肖杰
 */
@Controller
@RequestMapping(value = "/admin")
public class AdminController extends BaseController {

    @Resource
    private AdminService adminService;
    private static final String REL = "adminList";// 管理员列表navTab

    @RequestMapping(value = "/list")
    public String List(Integer pageNum, String status, ModelMap model, Integer numPerPage) {
        QueryParams params = new QueryParams();
        Map<String, Object> otherParams = new HashMap<String, Object>();
        otherParams.put("status", status == null ? 1 : status);
        params.setOtherParams(otherParams);
        Page<Admin> page = new Page<Admin>();
        page.setParams(params);
        if (numPerPage != null && numPerPage != 0) {
            page.setNumPerPage(numPerPage);
        }
        if (pageNum != null) {
            page.setPageNum(pageNum);
        }
        page = adminService.findAllByPaging(page);
        model.addAllAttributes(page.toMap(REL));
        return "admin/list";
    }

    @RequestMapping(value = "/show/{id}")
    public String get(@PathVariable("id") String id, ModelMap model) {
        model.addAttribute("admin", adminService.findById(id));
        return "admin/alter";
    }

    @RequestMapping(value = "/vali", method = RequestMethod.GET)
    @ResponseBody
    public boolean vali(String account) {
        return adminService.validation(account);
    }

    @RequestMapping(value = "/delete/{id}")
    @ResponseBody
    public Map<String, String> delete(@PathVariable("id") String id) {
        adminService.deleteByStatus(id);
        return ReturnValue.operateOK(ReturnValue.DELETE_OK, REL);
    }

    @RequestMapping(value = "/recover/{id}")
    @ResponseBody
    public Map<String, String> recover(@PathVariable("id") String id) {
        adminService.recover(id);
        return ReturnValue.operateOK(ReturnValue.RESET_OK, REL);
    }

    @RequestMapping(value = "/reset/{id}")
    @ResponseBody
    public Map<String, String> resetPass(@PathVariable("id") String id) {
        adminService.resetPassword(id);
        return ReturnValue.operateOK(ReturnValue.RESET_PASSWORD_OK, REL);
    }

    @RequestMapping(value = "/toAdd")
    public String toAdd() {
        return "admin/add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> add(Admin admin) {
        adminService.add(admin);
        return ReturnValue.closeCurrent(ReturnValue.OPERATE_OK, REL);
    }

    @RequestMapping(value = "/alter", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> alter(Admin admin) {
        adminService.update(admin);
        return ReturnValue.closeCurrent(ReturnValue.OPERATE_OK, REL);
    }
}
